﻿using BaseUi.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BaseUi
{
    /// <summary>
    /// 用户登录
    /// </summary>
    public partial class UserLoginView : Window
    {
        UserInfoViewModel viewModel = UserInfoView.viewModel;
        public UserLoginView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }

        private void load(object sender, RoutedEventArgs e)
        {
            //登录记忆
            if (LocalFile.Config.Sys.IsSaveLogin && LocalFile.Config.Sys.SaveLogin.Any())
            {
                ComboBoxId.ItemsSource = LocalFile.Config.Sys.SaveLogin;
                ComboBoxId.SelectedIndex = 0;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var loginName = ComboBoxId.Text.Trim();
            var pass = PasswordBoxPass.Password.ToString();

            if (string.IsNullOrEmpty(loginName))
            {
                HandyControl.Controls.MessageBox.Warning("请输入账号！", "提示");
                return;
            }
            if (string.IsNullOrEmpty(pass))
            {
                //HandyControl.Controls.MessageBox.Warning("请输入密码！", "提示");
                //return;
            }

            try
            {
                viewModel.User = AuthDb.db.Queryable<UserBase>().First(o => o.LoginName == loginName);
                if (viewModel.User == null)
                {
                    HandyControl.Controls.MessageBox.Warning("账号不存在！", "提示");
                    return;
                }
                if (!string.IsNullOrEmpty(viewModel.User.Password) && viewModel.User.Password != pass)
                {
                    HandyControl.Controls.MessageBox.Warning("密码错误！", "提示");
                    return;
                }

                //登录记忆
                if (LocalFile.Config.Sys.IsSaveLogin)
                {
                    LocalFile.Config.Sys.SaveLogin.RemoveAll(t => t == loginName);
                    LocalFile.Config.Sys.SaveLogin.Insert(0, loginName);
                    LocalFile.Config.Sys.SaveLogin = LocalFile.Config.Sys.SaveLogin.Take(LocalFile.Config.Sys.SaveLoginCount).ToList();
                    LocalFile.SaveConfig();
                }

                if (viewModel.User.IsAdmin)
                    viewModel.Roles = AuthDb.db.Queryable<RoleBase>().ToList();
                else
                    viewModel.Roles = viewModel.User.GetRoles();

                viewModel.IsLogin = true;
                this.DialogResult = true;
                this.Close();
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message, "错误");
                return;
            }
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Button_Click(null, null);
            }
            else if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        /// <summary>
        /// 不登录，直接使用最高权限
        /// </summary>
        public static void NotLogin()
        {
            UserInfoView.viewModel.User = AuthDb.db.Queryable<UserBase>().First(o => o.LoginName == "admin");
            UserInfoView.viewModel.Roles = AuthDb.db.Queryable<RoleBase>().ToList();
            UserInfoView.viewModel.IsLogin = true;
        }
    }
}
